 //
//  Status(微博模型).swift
//  我的微博
//
//  Created by Creator on 15/10/14.
//  Copyright © 2015年 Creator. All rights reserved.
//

import UIKit
import SDWebImage


///  微博模型
class Status: NSObject {
    
    ///   微博创建时间
    var created_at: String?
    ///   微博ID 
    var id: Int = 0
    ///   微博信息内容
    var text: String?
    ///  微博来源
    var source: String?
    ///  配图数组
    var pic_urls: [[String : AnyObject]]?
        {
        didSet {
            // 判断数组是否有数据
            if pic_urls!.count == 0 {
                return
            }
            // 实例化数组
            storedPictureURLs = [NSURL]()
            storedLargePictureURLs = [NSURL]()
            
            // 遍历字典生成 url 的数组
            for dict in pic_urls! {
            
                if let urlString = dict["thumbnail_pic"] as? String {
                    // 生成缩略图的 URL
                    storedPictureURLs?.append(NSURL(string: urlString)!)
                    // 生成大图的 URL
                    let largeURLSring = urlString.stringByReplacingOccurrencesOfString("thumbnail", withString: "large")
                    storedLargePictureURLs?.append(NSURL(string: largeURLSring)!)
                }
            }
        }
    }
    
    ///  保存配图的URL数组
    private var storedPictureURLs: [NSURL]?
    
 /// 保存配图的大图 URL 的数组
    private var storedLargePictureURLs: [NSURL]?
    
    ///  配图的URL的数组
    var pictureURLs: [NSURL]? {
    
        return retweeted_status == nil ? storedPictureURLs : retweeted_status?.storedPictureURLs
    }
    
    ///  返回大图的 URL
    var largePictureURLs: [NSURL]? {
    
        return retweeted_status == nil ? storedLargePictureURLs : retweeted_status?.storedLargePictureURLs
    }

    ///   用户
    var user: User?
    
    ///  转发微博
    var retweeted_status: Status?
    
    ///  微博所需行高
    var rowHeight: CGFloat?
    
    ///  懒加载微博数组
    class func loadStatus(since_id: Int, max_id: Int, finished: (dataList: [Status]?, eror: NSError?) -> ()) {
        
        NetworkTools.sharedTools.loadStatus(since_id, max_id: max_id) { (result, error) -> () in
            
            if error != nil {
                finished(dataList: nil, eror: error)
                return
            }
        ///  判断能否获得字典数组
            if let array = result?["statuses"] as? [[String : AnyObject]] {
            
                // 遍历数组， 字典转模型
                var list = [Status]()
                
                for dict in array {
                    list.append(Status(dict: dict))
                }
                
                // 获得完整的微博数组，可以回调
                self.cachWebImage(list, finished: finished)

                
            }else {
                finished(dataList: nil, eror: nil)
            }
        }
    }
    
    
    ///  缓存微博图片
    private class func cachWebImage(list: [Status], finished: (dataList: [Status]?, error: NSError?) ->()) {
    
        // 创建调度组
        let group = dispatch_group_create()
        // 缓存图片的大小
        var dataLength = 0
        
        // 遍历数组
        for status in list {
        
            // 判断是否有图片
            guard let urls = status.pictureURLs else {
                // urls 为空
                continue
            }
        // 遍历urls的数组 SDWebImage 缓存图片
            for imageUrl in urls {
            
                // 入组
                dispatch_group_enter(group)
                SDWebImageManager.sharedManager().downloadImageWithURL(imageUrl, options: SDWebImageOptions(rawValue: 0), progress: nil, completed: { (image, _, _, _, _) -> Void in
                    
                    // 将像转换成二进制数据
                    let data = UIImagePNGRepresentation(image)!
                    
                    dataLength += data.length
                    
                    // 离组
                    dispatch_group_leave(group)
                    
                })
                
            }
            
        }
        
        // 监听所有缓存操作的通知
        dispatch_group_notify(group, dispatch_get_main_queue()) {
            // 获得完整的新浪微博数组  可以回调
            
            print("缓存图片大小\(dataLength / 1024) K")
            
            finished(dataList: list, error: nil)
        }
    }
    
//  MARK: - 构造函数
    init(dict: [String : AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        // 判断 key 是否是 user 是的话就单独处理
        if key == "user" {
            // 判断 value 是否是一个有效的字典
            if let dict = value as? [String : AnyObject] {
                // 创建用户数据
                user = User(dict: dict)
            }
            return
        }
        
        // 判断 key 是否是 retweeted_status 是否为空
        // 转发微博最多只会执行一次
        if key == "retweeted_status" {
            if let dict = value as? [String : AnyObject] {
                retweeted_status = Status(dict: dict)
            }
            return
        }
        
        
        super.setValue(value, forKey: key)
    }
    
    override var description: String {
    
        let keys = ["created_at", "id", "text", "source", "pic_urls"]
        
        return "\(dictionaryWithValuesForKeys(keys))"
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}

}
